package com.dsteiner.xelavos.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import com.dsteiner.xelavos.client.domain.User;
import com.dsteiner.xelavos.server.dao.DataDao;
import com.dsteiner.xelavos.server.domain.File;
import com.google.appengine.api.datastore.Blob;

public class ImageServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			String id = req.getParameter("id");
			try {
				File file = (File) DataDao.dataDao.getObjectById(id, File.class);
				if(file != null){
					Blob image = file.getBlob();
					resp.setContentType("image/jpeg");
					resp.getOutputStream().write(image.getBytes()); 
				}
			} finally {
			}
		} catch (Exception ex) {

			throw new ServletException(ex);
		}
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		System.out.println("ImageServlet.doPost()");
	}
}